// miniprogram/pages/index/index.js
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');

var qqmapsdk;
const {
  mainViewPic
} = require('../../api/api')
const {
  checkLogin
} = require('../../utils/util')
const {
  decodeUserInfo
} = require('../../api/login')


const {
  abnormalList
} = require('../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {

    navHeight: getApp().globalData.navHeight,
    navTop: getApp().globalData.navTop,
    menuHeight: getApp().globalData.menuHeight,
    loginVisible: true,
    mainHeight: getApp().globalData.mainHeight,
    currentIndex: 0,
    list: [],
    page: 1,
    limit: 10,
    total: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    wx.hideTabBar()
    getApp().editTabbar()
    this.loadData()
  },
  onShow() {
    checkLogin('none').then((res) => {
      const {
        token
      } = this.data
      if (token === wx.getStorageSync('token')) {
        return
      }
      this.setData({
        token: wx.getStorageSync('token'),
        page: 1,
        list: []
      })
      this.loadData()
    }).catch((e) => {
      wx.showModal({
        title: '提示',
        content: '请先登录，以便获取数据',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/index',
            })
          }
        }
      })
    })
  },
  async loadData() {
    const {
      page,
      limit,
      currentIndex
    } = this.data
    const params = {
      page,
      limit,
      state: currentIndex == 0 ? 'A' : 'D'
    }
    checkLogin('none').then(async (res) => {
      const result = await abnormalList(params)
      if (result.length < 1) {
        return
      }
      this.setData({
        ['list[' + (page - 1) + ']']: result,
        page: page + 1,
        total: result.length
      })
    })



  },
  changeIndex(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      currentIndex: parseInt(index),
      list: [],
      page: 1,
      total:0
    })
    this.loadData()
  },
  getSystemInfo: function () {
    let t = this;
    wx.getSystemInfo({
      success: function (res) {
        const windowHeight = res.windowHeight
        wx.setStorageSync('windowHeight', windowHeight)
        t.setData({
          windowHeight
        })
      }
    });
  },

  hideModal(e) {
    this.setData({
      loginVisible: false
    })
  },
  goto(e) {
    const {
      index,
      type,
      url
    } = e.currentTarget.dataset
    if (type === 'nav') {
      if (index === 6) {
        if (!this.checkSubject()) {
          return
        }
      }
      wx.navigateTo({
        url: url,
      })
    } else if (type === 'tab') {
      wx.switchTab({
        url: url,
      })
    }
  },

  scroll(e) {
    const scrollTop = e.detail.scrollTop

    if (scrollTop > 80) {
      this.setData({
        isBack: true
      })
    } else {
      this.setData({
        isBack: false
      })
    }
  },

  inputChange(e) {
    this.setData({
      score: e.detail.value
    })
  },

})